<?

function validip($ip)
{
	if (!empty($ip) && $ip == long2ip(ip2long($ip)))
	{
		// reserved IANA IPv4 addresses
		// http://www.iana.org/assignments/ipv4-address-space
		$reserved_ips = array (
				array('0.0.0.0','2.255.255.255'),
				array('10.0.0.0','10.255.255.255'),
				array('127.0.0.0','127.255.255.255'),
				array('169.254.0.0','169.254.255.255'),
				array('172.16.0.0','172.31.255.255'),
				array('192.0.2.0','192.0.2.255'),
				array('192.168.0.0','192.168.255.255'),
				array('255.255.255.0','255.255.255.255')
		);

		foreach ($reserved_ips as $r)
		{
				$min = ip2long($r[0]);
				$max = ip2long($r[1]);
				if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max)) return false;
		}
		return true;
	}
	else return false;
}

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	$input = $_POST["input"];
	if (validip($input))
	{
	$long = ip2long($input);
	if ($long == -1)
	print("Error: Bad IP/long");
	else
	print("Long Result: $long");
	}
	else
	{
	$ip = long2ip($input);
	if (!validip($ip))
	print("Error: Bad IP/long");
	else
	print("IP Result: $ip");
	}
	exit();
}

?>


<html><head>
<title>IP Conversion</title>
</head>
<body>
<h1>IP Conversion</h1>
<form method=post action=ip2long.php>
<table border=1 cellspacing=0 cellpadding=5>
<tr><td class=rowhead>IP/long</td><td><input type=text name=input></td></tr>
<tr><td colspan=2 align=center><input type=submit class=btn value='OK'></td></tr>
</form>
</table>
</body>
</html>